<?php
/**数据统计类
 * @explam 
 * $stat=new _Stat($db,$keywords);
 * $stat->start();
 * @author zhutl
 *
 */
class _Stat{
	//public $TmpDir=TMPDIR;
	//public $UserDataDir;
	//public $UserData;
	//public $UserTodayDir;
	public $Time;
	public $Year;
	public $Month;
	public $Day;
	public $Week;
	public $Hour;
	public $keyword;
	public $DayStartTime;
	public $DayStartTimeSave;
	public $NewDay = false;
	private $db;

/**
 * @param unknown_type $db 数据库类
 * @param unknown_type $keyword 需要统计的关键字
 */
public  function __construct($db,$keyword){
		$this->db = $db;
		$this->Time = time();
		$this->Year = date( 'Y', $this->Time );
		$this->Month = date( 'm', $this->Time );
		$this->Day = date( 'j', $this->Time );
		$this->Week = date( 'w', $this->Time );
		$this->Hour = date('G', $this->Time );
		$this->keyword=$keyword;
		$this->DayStartTime = mktime(0, 0, 0, $this->Month, $this->Day, $this->Year);
		//$this->DayStartTimeSave = $this->DayStartTime - 24*60*60*SAVE_OLD_DAYS;
		
		
	}
	
	public function start(){
				
		$this->CheckNewDay();

		$sql="SELECT datadayid
		FROM  `bs_day_data`
		WHERE times=$this->DayStartTime
		and keyword='$this->keyword'	
		";
		//echo $sql;exit;
		$DayData = $this->db->find($sql);
		
		if(empty($DayData)){
			$DayData[0]->datadayid=$this->IniToday();	
			//exit;		
		}
		$datadayid=$DayData[0]->datadayid;
		//echo $datadayid;exit;
		$UpdateStr = "day_count = day_count + 1, hour{$this->Hour} = hour{$this->Hour} + 1";
		
		$sql='UPDATE `bs_day_data` set '.$UpdateStr.' WHERE `datadayid`='.$datadayid;
		//echo $sql;exit;
		$this->db->query($sql);
			
	}
	function ClearYesterday()
	{
		
		$YesterdayStartTime = mktime (0, 0, 0, $this->Month, $this->Day-1, $this->Year);
		
		$YesterdayYear = date( 'Y', $YesterdayStartTime );
		$YesterdayMonth = date( 'm', $YesterdayStartTime );
		$YesterdayDay = date( 'j', $YesterdayStartTime );
		$YesterdayWeek = date( 'w', $YesterdayStartTime );
		$YesterdayHour = date('G', $YesterdayStartTime );
	
		$Res = $this->db->find( " select * from `bs_day_data`
				 where times = $YesterdayStartTime and  keyword = '$this->keyword'" );
		$YesterdayData = $Res[0];
		//print_r($YesterdayData);exit;
		$YesterdayData->day_count = intval( $YesterdayData->day_count );
		
	
		//Update Month Table
		$YesterdayMonthTime = mktime (0,0,0,$YesterdayMonth, 1, $YesterdayYear );
		$Res = $this->db->find( " select datamonthid from `bs_month_data`
				 where times = $YesterdayMonthTime and keyword = '$this->keyword'" );
		
		if( empty($Res[0]) )
		{
			$data=array(
					'keyword'=>$this->keyword,
					'times'=>$YesterdayMonthTime,					
					'year'=>$this->Year,					
					'month'=>$this->Month,
					//'month_count'=>	$YesterdayData->day_count
					);
			$this->db->save('bs_month_data',$data);	
			//ECHO AAA;EXIT;	
		}
		else
		{
			$UpdateStr = "month_count = month_count + $YesterdayData->day_count,
		    month_count_ip = month_count_ip + $YesterdayData->day_count_ip";
			
			$sql='UPDATE `bs_month_data` set '.$UpdateStr.' WHERE `datamonthid`='.$Res[0]->datamonthid;
			$this->db->query($sql);
			//echo $sql;exit;
			//print_r($this->db->query($sql));exit;
		}
		//exit;
		//Update Year Table
		$YesterdayYearTime = mktime ( 0, 0, 0, 1, 1, $YesterdayYear );
		
		$Res = $this->db->find( " select datayearid from `bs_year_data` 
				where times = $YesterdayYearTime and keyword = '$this->keyword'" );		
		
		
		if(empty($Res[0]))
		{
			//echo $YesterdayYearTime;EXIT;
			$data=array(
					'keyword'=>$this->keyword,
					'times'=>$YesterdayYearTime,
					'year'=>$YesterdayYear,
					'year_count'=>$YesterdayData->day_count
					//'month_count'=>	$YesterdayData->day_count
			);
			//print_r($data);exit;
			$this->db->save('bs_year_data',$data);
			
			
		}
		else
		{
			
			$UpdateStr = "year_count = year_count + $YesterdayData->day_count,
			year_count_ip = year_count_ip + $YesterdayData->day_count_ip";
				
			$sql='UPDATE `bs_year_data` set '.$UpdateStr.' WHERE `datayearid`='.$Res[0]->datayearid;
			
			$this->db->query($sql);
			
		}
	
	}
	
	function CheckNewDay()
		{
						
		$sql="SELECT datadayid,times
		FROM  `bs_day_data`
		WHERE  `keyword` =  '$this->keyword'		
		ORDER BY datadayid 
		LIMIT 1		
		";		
		//echo $sql;
		$rows = $this->db->find($sql);
			//print_r($rows);
		if((time()-$rows[0]->times)>=86400){
				$this->NewDay = true;	
				$this->ClearYesterday();	
				$this->IniToday();
			}
			
		}
	function IniToday()
		{
			$data=array(
					'keyword'=>$this->keyword,
					'times'=>$this->DayStartTime,
					'year'=>$this->Year,
					'month'=>$this->Month,
					'day'=>$this->Day,
					'week'=>$this->Week
			);
			$this->db->save('bs_day_data',$data);
			return $data['id'];
		}


}

